package com.atguigu.guli.servicecms.service.impl;

import com.atguigu.guli.servicecms.entity.Banner;
import com.atguigu.guli.servicecms.mapper.BannerMapper;
import com.atguigu.guli.servicecms.service.BannerService;
import com.atguigu.servicebase.exception.Assert;
import com.atguigu.servicebase.result.ResponseEnum;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * 首页banner表 服务实现类
 * </p>
 *
 * @author atguigu
 * @since 2021-08-05
 */
@Service
public class BannerServiceImpl extends ServiceImpl<BannerMapper, Banner> implements BannerService {

    @Cacheable(value = "banner", key = "'IndexList'")
    @Override
    public List<Banner> selectIndexList() {
        List<Banner> list = baseMapper.selectList(new QueryWrapper<Banner>().orderByDesc("sort"));
        return list;
    }


    @CacheEvict(value = "banner", allEntries=true)
    @Override
    public void saveBanner(Banner banner) {
        int insert = baseMapper.insert(banner);
        Assert.notZero(insert, ResponseEnum.INSERT_ERROR);
    }

    @CacheEvict(value = "banner", allEntries=true)
    @Override
    public void updateBanner(Banner banner) {
        int i = baseMapper.updateById(banner);
        Assert.notZero(i, ResponseEnum.UPDATE_ERROR);
    }

    @CacheEvict(value = "banner", allEntries=true)
    @Override
    public void removeBanner(String id) {
        int i = baseMapper.deleteById(id);
        Assert.notZero(i, ResponseEnum.DELETE_ERROR);
    }
}
